package com.im.app.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.jim.core.packets.ChatBody;
import org.jim.core.packets.ChatType;
import org.jim.core.utils.JsonKit;

import java.util.Date;

@Data
@TableName("chat_logs")
public class ChatLogs {
    @TableId(type = IdType.ASSIGN_UUID)
    private String msgId;
    //发送id
    private String sendId;
    //接收id
    private String recvId;
    //发送者昵称
    private String nickName;
    //发送者头像
    private String avatar;
    //1群聊 2私聊
    private Integer chatType;
    //1 系统消息 2 用户消息
    private Integer fromType;
    //消息类型 0:text、1:image、2:video、3:file、4:location
    private Integer msgType;
    //是否已读  0 未读 1 已读
    private Integer isRead;
    //内容
    private String content;
    //状态
    private Integer status;
    //发送时间
    private Long sendTime;
    //创建时间
    private Long createTime;

    private String ex;

    public ChatLogs() {

    }
    public ChatLogs(String recvId) {
        this.recvId = recvId;
    }


    public static ChatLogs bodyToLog(ChatBody chatBody){
        ChatLogs chatLogs = new ChatLogs();
        chatLogs.setMsgId(chatBody.getId());
        chatLogs.setAvatar(chatBody.getAvatar());
        chatLogs.setNickName(chatBody.getNickName());
        chatLogs.setMsgType(chatBody.getMsgType());
        chatLogs.setSendId(chatBody.getFrom());
        chatLogs.setCreateTime(new Date().getTime());
        chatLogs.setContent(chatBody.getContent());
        chatLogs.setFromType(chatBody.getFromType());
        chatLogs.setIsRead(0);
        chatLogs.setStatus(2);
        chatLogs.setSendTime(chatBody.getCreateTime());
        //消息类型 群聊/私聊 2-私聊 1-群聊
        chatLogs.setChatType(chatBody.getChatType());
        if(chatBody.getExtras() != null){
            String json = JsonKit.toJSONString(chatBody.getExtras());
            chatLogs.setEx(json);
        }
        if(chatBody.getChatType() == ChatType.CHAT_TYPE_PUBLIC.getNumber()){
            //设群id
            chatLogs.setRecvId(chatBody.getGroupId());
        }else if(chatBody.getChatType() == ChatType.CHAT_TYPE_PRIVATE.getNumber()){
            chatLogs.setRecvId(chatBody.getTo());
        }
        return chatLogs;
    }
}
